﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Light.Utility.Security
{

    /// <summary>
    /// 公式加密（用于加密数值型数据）
    /// </summary>
    public class FormulaCryptor
    {

        /// <summary>
        /// 加密公式 
        /// </summary>
        /// <param name="x">要加密的数据</param>
        /// <param name="strKey">数值型密钥</param>
        /// <returns>10位精度</returns>
        public static decimal Encrypt(decimal x, string strKey)
        {

            int pow = strKey.Length;
            double key = double.Parse(strKey);
            double div = Math.Pow(10, pow);
            double cipherValue = Math.Round(((double)x) * (0.3141592653 + key / div), 10);

            return (decimal)cipherValue; 

        }

        /// <summary>
        /// 解密公式 
        /// </summary>
        /// <param name="cipher">加密后的数据</param>
        /// <param name="strKey">数值型密钥</param>
        /// <returns>4位精度</returns>
        public static decimal Decrypt(decimal cipher, string strKey)
        {

            double cipherValue = double.Parse(cipher.ToString());

            int pow = strKey.Length;
            double key = double.Parse(strKey);
            double div = Math.Pow(10, pow);
            double platValue = Math.Round(cipherValue / (0.3141592653 + key / div), 15);

            return (decimal)platValue;

        }
    }
}
